/**
 * @author lk
 * @date 2022/6/7 13:05
 */
package com.mmall.thread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipTest {
    public static void main(String[] args) throws IOException {
        String fileName = "05-learn-java/src/main/java/com/mmall/thread/thread.zip";
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(fileName));
        ZipEntry nextEntry = zipInputStream.getNextEntry();
        while (nextEntry != null) {
            System.out.println("nextEntry.getName() = " + nextEntry.getName());
            System.out.println("nextEntry.getCompressedSize() = " + nextEntry.getCompressedSize());
            nextEntry = zipInputStream.getNextEntry();
        }
    }
}

class FilesTest {
    public static void main(String[] args) throws IOException {
        String fileName = "05-learn-java/src/main/java/com/mmall/thread/thread.zip";
        FileInputStream fileInputStream = new FileInputStream(fileName);
        Path of = Path.of("some");
//        Files.copy(fileInputStream, of);
//        Path some = Path.of("./some");
//        System.out.println("Files.exists(some) = " + Files.exists(some));
//        System.out.println("some.resolve(\"rela\") = " + some.resolve("rela"));
        unzip(new ZipInputStream(fileInputStream), of);
    }
    public static void unzip( ZipInputStream zis,  Path targetPath)
            throws IOException {
        // 1. unzip file to folder
        // 2. return the folder path

        ZipEntry zipEntry = zis.getNextEntry();

        while (zipEntry != null) {
            // Resolve the entry path
            Path entryPath = targetPath.resolve(zipEntry.getName());


            if (zipEntry.isDirectory()) {
                // Create directories
                Files.createDirectories(entryPath);
            } else {
                // Copy file
                Files.copy(zis, entryPath);
            }

            zipEntry = zis.getNextEntry();
        }
    }
}